package ms.shop.controller;

import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.string.StringUtil;
import ms.shop.bean.AttrParamBean;
import ms.shop.bean.CommonQueryParamBean;
import ms.shop.config.ResponseCode;
import ms.shop.entity.Attr;
import ms.shop.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Project Name: ms
 * File Name: AttrController
 * Package Name: ms.shop.controller
 * Date: 2018/6/4  10:30
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@RestController
@RequestMapping("/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    /**
     * 分页查询
     * @param commonQueryParamBean
     * @return
     */
    @PostMapping("/query")
    public Page<Attr> query(@RequestBody CommonQueryParamBean commonQueryParamBean) {
        if(StringUtil.isEmpty(commonQueryParamBean.getPageNo())) {
            commonQueryParamBean.setPageNo(1);
        }
        if(StringUtil.isEmpty(commonQueryParamBean.getPageSize())) {
            commonQueryParamBean.setPageSize(10);
        }

        Page<Attr> page = attrService.query(commonQueryParamBean);
        return page;
    }

    /**
     * 查询列表
     * @param attrParamBean
     * @return
     */
    @PostMapping("/list")
    public List<Attr> query(@RequestBody AttrParamBean attrParamBean) {
        return attrService.list(attrParamBean);
    }


    /**
     * 新增
     * @param attrParamBean
     * @return
     */
    @PostMapping("/create")
    public void create(@RequestBody AttrParamBean attrParamBean) {
        //参数校验
        checkParam(attrParamBean);

        attrService.create(attrParamBean);
    }

    /**
     * 修改
     * @param attrParamBean
     * @return
     */
    @PostMapping("/update")
    public void update(@RequestBody AttrParamBean attrParamBean) {
        //参数校验
        checkParam(attrParamBean);

        attrService.update(attrParamBean);
    }


    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/use/{id}")
    public void use(@PathVariable("id") Long id) {
        attrService.use(id);
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable("id") Long id) {
        attrService.cancel(id);
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    public Attr detail(@PathVariable("id") Long id) {
        return attrService.detail(id);
    }

    /**
     * 清除缓存
     * @return
     */
    @PostMapping("/clear")
    public void clear() {
        attrService.clear();
    }

    /**
     * 校验新增和修改参数
     * @param attrParamBean
     */
    private void checkParam(AttrParamBean attrParamBean) {
        if(StringUtil.isEmpty(attrParamBean.getClassifyId())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "分类不能为空");
        }
        if(StringUtil.isEmpty(attrParamBean.getKeyName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "属性名不能为空");
        }
        if(StringUtil.isEmpty(attrParamBean.getKeyValue())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "属性值不能为空");
        }

    }
}
